{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1+2j)\n",
      "c1实部: 1.0\n",
      "c1虚部: 2.0\n",
      "(2+5j)\n",
      "c2实部: 2.0\n",
      "c2虚部: 5.0\n",
      "(1-2j)\n"
     ]
    }
   ],
   "source": [
    "# 1. 使用内置类创建对象\n",
    "# complex 创建复数对象\n",
    "\n",
    "# 复数 1+2i \n",
    "# complex 是一个复数类\n",
    "# c1 就是一个复数对象\n",
    "c1 = complex(1,2)  # 1+2i\n",
    "print(c1)\n",
    "print(\"c1实部:\",c1.real)  # real属性  类中定义属性\n",
    "print(\"c1虚部:\",c1.imag)  #\n",
    "c2 = complex(2,5)  # 2+5i\n",
    "print(c2)\n",
    "print(\"c2实部:\",c2.real)\n",
    "print(\"c2虚部:\",c2.imag)\n",
    "\n",
    "c3 = c1.conjugate() # conjugate() 类中定义方法\n",
    "print(c3)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "当前复数对象总数量 1\n",
      "当前复数对象总数量 1\n",
      "1\n",
      "-2\n",
      "当前复数对象总数量 2\n"
     ]
    }
   ],
   "source": [
    "class Complex2:\n",
    "\n",
    "    # 类属性\n",
    "    count = 0\n",
    "\n",
    "    # 类的构造方法__init__\n",
    "    def __init__(self,real,imag):\n",
    "        # 实例对象属性\n",
    "        self.real = real\n",
    "        self.imag = imag\n",
    "        # 类属性 (通过类名调用，也可以使用对象调用)\n",
    "        Complex2.count += 1\n",
    "\n",
    "    # 自定义方法\n",
    "    def conjugate(self):\n",
    "        return Complex2(self.real,-self.imag)\n",
    "    \n",
    "\n",
    "    \n",
    "c11 = Complex2(1,2)\n",
    "print(c11.real)\n",
    "print(c11.imag)\n",
    "print(\"当前复数对象总数量\",Complex2.count) # 用类名调用类属性\n",
    "print(\"当前复数对象总数量\",c11.count) # 也可以用实例调用了类属性，但不推荐\n",
    "c12 = c11.conjugate()\n",
    "print(c12.real)\n",
    "print(c12.imag)\n",
    "print(\"当前复数对象总数量\",Complex2.count)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "小白\n",
      "1\n",
      "小黑\n",
      "2\n",
      "小白 抓老鼠\n",
      "小黑 抓老鼠\n",
      "小白 :zzzz\n",
      "小黑 :zzzz\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# 定义类\n",
    "class Cat:\n",
    "    def __init__(self,name,age):\n",
    "        self.name = name\n",
    "        self.age = age\n",
    "    def catchMouse(self):\n",
    "        print(self.name,\"抓老鼠\")\n",
    "    def eat_food(self):\n",
    "        print(self.name,\"吃东西\")\n",
    "    def sleep(self):\n",
    "        print(self.name,\":zzzz\")\n",
    "        \n",
    "# 创建对象\n",
    "c1 = Cat('小白',1)\n",
    "print(c1.name)\n",
    "print(c1.age)\n",
    "# print(c1.color)\n",
    "\n",
    "c2 = Cat('小黑',2)\n",
    "print(c2.name)\n",
    "print(c2.age)\n",
    "\n",
    "c1.catchMouse();\n",
    "c2.catchMouse();\n",
    "c1.sleep()\n",
    "c2.sleep()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "张三 男\n",
      "张三 正在学习\n"
     ]
    }
   ],
   "source": [
    "# 练习 参考猫类\n",
    "# 定义一个学生类 要有一些属性 一些方法\n",
    "# 使用你定义的类创建2个学生\n",
    "# 让这2个学生执行学生的方法\n",
    "\n",
    "\n",
    "# 定义类\n",
    "class Student:\n",
    "\n",
    "    # 定义构造函数\n",
    "    def __init__(self,name,sex):\n",
    "        self.name = name\n",
    "        self.sex = sex\n",
    "\n",
    "    # 定义方法\n",
    "    def study(self):\n",
    "        print(self.name,\"正在学习\")\n",
    "\n",
    "\n",
    "# 创建对象\n",
    "s1 = Student('张三','男')\n",
    "print(s1.name,s1.sex)\n",
    "s1.study()\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1+2i\n",
      "2+3i\n",
      "3+5i\n"
     ]
    }
   ],
   "source": [
    "class Complex2:\n",
    "\n",
    "    # 类属性\n",
    "    count = 0\n",
    "\n",
    "    # 类的构造方法__init__\n",
    "    def __init__(self,real,imag):\n",
    "        # 实例对象属性\n",
    "        self.real = real\n",
    "        self.imag = imag\n",
    "        # 类属性 (通过类名调用，也可以使用对象调用)\n",
    "        Complex2.count += 1\n",
    "\n",
    "    # 自定义方法\n",
    "    def conjugate(self):\n",
    "        return Complex2(self.real,-self.imag)\n",
    "    \n",
    "    # 打印对象会调用这个方法\n",
    "    def __str__(self):\n",
    "        # 这里不要写打印语句 是要将打印的内容作为字符串返回\n",
    "        return str(self.real) + \"+\" + str(self.imag) + \"i\"\n",
    "    \n",
    "    # 实现+运算符\n",
    "    def __add__(self,other):\n",
    "        r = self.real + other.real\n",
    "        i = self.imag + other.imag\n",
    "        return Complex2(r,i)\n",
    "        \n",
    "c1 = Complex2(1,2)\n",
    "print(c1)\n",
    "c2 = Complex2(2,3)\n",
    "print(c2)\n",
    "c3 = c1 + c2\n",
    "print(c3)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "练习2 \n",
    "1. 定义一个分数类 \n",
    "\n",
    "2. 创建2个分数对象  1/2  1/3\n",
    "\n",
    "3. 定义 __str__方法， 打印对象  1/2  \n",
    "\n",
    "4. 定   __add__  实现 分数相加\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
